package com.example.java_chatroom1.controller;

import com.example.java_chatroom1.model.MessageInfo;
import com.example.java_chatroom1.model.Result;
import com.example.java_chatroom1.server.MessageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/messageInfo")
public class MessageInfoController {
    @Autowired
    private MessageInfoService messageInfoService;

    @RequestMapping("/getLastMessageBySessionId")
    public Result<String> getLastMessageBySessionId(Integer sessionId) {
        String message = messageInfoService.getLastMessageBySessionId(sessionId);
        if (message == null) return Result.success("");
        return Result.success(message);
    }

    @RequestMapping("/getMessageInfosBySessionId")
    public Result<List<MessageInfo>> getMessageInfosBySessionId(Integer sessionId) {
        List<MessageInfo> ret = messageInfoService.getMessageInfosBySessionId(sessionId);
        Collections.reverse(ret);
        return Result.success(ret);
    }
}
